using System;
using System.Windows.Forms;
using Sharpmark.PerfectLife.CommonFramework.Picture;
using Sharpmark.PerfectLife.CommonFramework.Picture.Utilities.ImageToText;

namespace Sharpmark.PerfectLife.ColorfulPhotos.TextualPhoto.SubFunctionForm
{
	public partial class FrmStringText : Form
	{
		private StringText _stringText;

		public FrmStringText ()
		{
			InitializeComponent();

			_stringText = new StringText(
				(int)nudCharsWidth.Value, (int)nudCharsHeight.Value,
				(txtBackgroundChar.Text != "") ? txtBackgroundChar.Text[0] : ReplaceCharsList.Chars2[1],
				(txtFrontChar.Text != "") ? txtFrontChar.Text[0] : ReplaceCharsList.Chars2[0],
				(txtContent.Text != "") ? txtContent.Text : this.ProductName,
				fdFontSelect.Font, ckbVerticalString.Checked);
		}

		private void btnGenerate_Click (object sender, EventArgs e)
		{
			//TODO:FontMustExist到底适不适用？
			System.Drawing.Font font = fdFontSelect.Font;

			txtGeneratedText.Text = _stringText.GenerateText();

			//TOOD:对特殊字符的检测;try catch()来做
			sfdSaveText.FileName = txtContent.Text
				+ "_" + this.txtFrontChar.Text
				+ "_" + this.txtBackgroundChar.Text;
		}

		private void btnFontSetting_Click (object sender, EventArgs e)
		{
			fdFontSelect.ShowDialog();
			_stringText.Font = fdFontSelect.Font;
		}

		private void textBoxValidate ()
		{
			if (txtContent.Text != ""
				&& txtBackgroundChar.Text != ""
				&& txtFrontChar.Text != "")
			{
				btnGenerate.Enabled = true;
			}
			else
			{
				btnGenerate.Enabled = false;
			}
		}

		private void textBox_SelectAll (object sender, EventArgs e)
		{
			((TextBox)sender).SelectAll();
		}

		private void numbericUpDown_SelectAll (object sender, EventArgs e)
		{
			NumericUpDown nup = (NumericUpDown)sender;
			nup.Select(0, nup.Value.ToString().Length);
		}

		private void btnSave_Click (object sender, EventArgs e)
		{
			if (sfdSaveText.ShowDialog() == DialogResult.OK)
			{
				// 写入文件
				System.IO.StreamWriter sw = new System.IO.StreamWriter(
					sfdSaveText.FileName, false);

				sw.Write(txtGeneratedText.Text);

				sw.Close();
			}
		}

		private void nudCharsWidth_ValueChanged (object sender, EventArgs e)
		{
			_stringText.WordWidth = (int)nudCharsWidth.Value;
		}

		private void nudCharsHeight_ValueChanged (object sender, EventArgs e)
		{
			_stringText.WordHeight = (int)nudCharsHeight.Value;
		}

		private void txtBackgroundChar_TextChanged (object sender, EventArgs e)
		{
			if (txtBackgroundChar.Text != "")
				_stringText.BackgroundChar = this.txtBackgroundChar.Text[0];

			textBoxValidate();
		}

		private void txtFrontChar_TextChanged (object sender, EventArgs e)
		{
			if (txtFrontChar.Text != "")
				_stringText.FrontChar = this.txtFrontChar.Text[0];

			textBoxValidate();
		}

		private void txtContent_TextChanged (object sender, EventArgs e)
		{
			if (txtContent.Text != "")
				_stringText.Content = this.txtContent.Text;

			textBoxValidate();
		}

		private void ckbVerticalString_CheckedChanged (object sender, EventArgs e)
		{
			_stringText.IsVertical = ckbVerticalString.Checked;
		}
	}
}